package com.guigu.dh.service.impl;

import com.guigu.dh.common.result.Result;
import com.guigu.dh.helper.HttpRequestHelper;
import com.guigu.dh.service.SmsService;
import com.guigu.dh.vo.msm.MsmVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author DengHao
 * @date 2022-03-08
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 存入redis
    @Override
    public Result receivePhone(String phone) {
        // 设置过期时间 3分钟
        redisTemplate.opsForValue().set(phone,phone,180, TimeUnit.SECONDS);
        return Result.ok();
    }

    // 判断是否存在
    @Override
    public Boolean hasPhone(String phone) {
        Boolean aBoolean = redisTemplate.hasKey(phone);
        // 返回是否存在标志
        return aBoolean;
    }


    /*@Override
    public boolean send(String phone, String code) {
        //判断手机号是否为空
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantYmlUtil.REGION_Id,
                        ConstantYmlUtil.ACCESS_KEY_ID,
                        ConstantYmlUtil.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "线上预约挂号平台");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_180051135");
        //验证码  使用json格式   {"code":"123456"}
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }*/

   /* // 使用网易云音乐接口发送验证码
    @Override
    public boolean send(String phone) {
        String url = "http://cloud-music-api-lyart.vercel.app/captcha/sent?phone=" + phone;
        String o = null;
        try {
            //o = HttpRequestHelper.sendGet(url);
             o = HttpRequestHelper.doPost(url, "{}", "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(o);
        return false;
    }*/

    /*@Override
    public boolean send(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())) {
            String code = (String) msmVo.getParam().get("code");
            return this.send(msmVo.getPhone(), code);
        }
        return false;
    }*/

}
